package com.buka.config;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.function.Predicate;

@Component
public class CustomRoutePredicateFactory extends AbstractRoutePredicateFactory<CustomRoutePredicateFactory.Config> {

    public CustomRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        // 自定义断言逻辑，例如检查请求头中的某个值
        return exchange -> {
            String headerValue = exchange.getRequest().getHeaders().getFirst("X-Custom-Header");
            return headerValue != null && headerValue.equals(config.headerValue);
        };
    }

    public static class Config {
        private String headerValue = "buka";

        public String getHeaderValue() {
            return headerValue;
        }

        public void setHeaderValue(String headerValue) {
            this.headerValue = headerValue;
        }
    }
}